package com.niuwa.p2p.service.impl.exchange;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.constant.Constants;
import com.niuwa.p2p.core.finance.CostCalculatorHelper;
import com.niuwa.p2p.core.finance.repay.LoanInfoVo;
import com.niuwa.p2p.core.finance.repay.Repay;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.utils.CalculateBusiUtils;
import com.niuwa.p2p.core.utils.DateUtil;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.core.utils.EmptyUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.dao.business.CustClaimDao;
import com.niuwa.p2p.dao.business.InvestProdRecordDao;
import com.niuwa.p2p.dao.business.InvestTransDao;
import com.niuwa.p2p.dao.business.LoanClaimDao;
import com.niuwa.p2p.dao.business.PlanProductDao;
import com.niuwa.p2p.dao.business.ReceivePlanDao;
import com.niuwa.p2p.dao.business.ReceiveRecordDao;
import com.niuwa.p2p.dao.business.RepayPlanDao;
import com.niuwa.p2p.dao.fund.AccountFlowDao;
import com.niuwa.p2p.dao.fund.BaseAccountDao;
import com.niuwa.p2p.dao.fund.CustAccountDao;
import com.niuwa.p2p.dao.fund.NwAccountDao;
import com.niuwa.p2p.dao.productTemplate.ProductDao;
import com.niuwa.p2p.dao.yeepay.YeePayLogDao;
import com.niuwa.p2p.entity.business.CustClaim;
import com.niuwa.p2p.entity.business.InvestProdRecord;
import com.niuwa.p2p.entity.business.InvestTrans;
import com.niuwa.p2p.entity.business.LoanClaim;
import com.niuwa.p2p.entity.business.PlanProduct;
import com.niuwa.p2p.entity.business.ReceivePlan;
import com.niuwa.p2p.entity.business.RepayPlan;
import com.niuwa.p2p.entity.fund.AccountFlow;
import com.niuwa.p2p.entity.fund.BaseAccount;
import com.niuwa.p2p.entity.fund.CustAccount;
import com.niuwa.p2p.entity.fund.NwAccount;
import com.niuwa.p2p.entity.yeepay.YeePayLogInfo;
import com.niuwa.p2p.entity.yeepay.supple.CompleteTransactionRequest;
import com.niuwa.p2p.entity.yeepay.supple.DirectTransactionResponse;
import com.niuwa.p2p.service.exchange.IFinishProductService;
import com.niuwa.p2p.service.sequence.ISequenceService;
import com.niuwa.p2p.service.yeepay.YeePayService;
/**
 * 结标
 * @title FinishServiceImpl.java
 *
 * @author liuhuan
 * @version 1.0
 * @created 2015年4月27日
 */
@Service
public class FinishServiceImpl implements IFinishProductService {
	//TODO 交易相关review - 结标放款
	
	@Autowired
	private LoanClaimDao loanClaimDao;
	@Autowired
	private ProductDao productDao;
	@Autowired
	private InvestProdRecordDao investProdRecordDao;
	@Autowired
	private CustClaimDao custClaimDao;
	@Autowired
	private CustAccountDao custAccountDao;
	@Autowired
	private BaseAccountDao baseAccountDao;
	@Autowired
	private AccountFlowDao accountFlowDao;
	@Autowired
	private ReceivePlanDao receivePlanDao;
	@Autowired
	private ReceiveRecordDao receiveRecordDao;
	@Autowired
	private PlanProductDao planProductDao;
	@Autowired
	private NwAccountDao nwAccountDao;
	@Autowired
	private RepayPlanDao repayPlanDao;
	@Autowired
	private YeePayLogDao yeePayLogDao;
	@Autowired
	private InvestTransDao investTransDao;
	
	@Autowired
	private ISequenceService sequenceService; 
	
	@Autowired
	private YeePayService yeePayService;
	
	/**
	 * 结标操作
	 * @param loanClaim : projectNo
	 * @param platForm: APP,P2P
	 * @param feeType: CHANNEL,APP
	 */
	@Override
	public JsonResult saveFinishProduct(LoanClaim lc,String platForm, String feeType) throws Exception {
		/**
		 * 调接口生成还款计划
		 * 借款入账
		 * 划转服务费（min,max）
		 * 生成用户债权
		 * 生成回款计划
		 * 投资人、借款人账户资金处理
		 * 流水生成
		 * 标的状态修改
		 */
		JsonResult result = new JsonResult();
		LoanClaim loanClaim = loanClaimDao.selectByPrimaryKey(lc.getProjectNo());
		if(EmptyUtil.isEmpty(loanClaim)){
			result.setMsg("结标项目数据有误");
			result.setSuccess(false);
			return result;
		}
		String releaseStatus = loanClaim.getReleaseStatus();
		if(releaseStatus.equals(DictConstant.ReleaseStatus.COMPLETE_SCALE.getCode())||releaseStatus.equals(DictConstant.ReleaseStatus.MATCHED.getCode())){
			result.setMsg("已经结标，请勿重复");
			result.setSuccess(false);
			return result;
		}
		//修改项目状态
		if(loanClaim.getProjectTo().equals(DictConstant.ProjectTo.FRONT.getCode())){
			loanClaim.setReleaseStatus(DictConstant.ReleaseStatus.COMPLETE_SCALE.getCode());
		} else if(loanClaim.getProjectTo().equals(DictConstant.ProjectTo.BACK.getCode())){
			loanClaim.setReleaseStatus(DictConstant.ReleaseStatus.MATCHED.getCode());
		}
		loanClaim.setLoanDate(new Date());
		if(loanClaim.getTimeType().equals("M")){
			Date d = DateUtil.getNextMonthTime(loanClaim.getLoanTerm(), DictConstant.TimeFormat.SHORTTIME.getCode());
			loanClaim.setExpireDate(DateUtil.getNextDayTime(-1, DictConstant.TimeFormat.SHORTTIME.getCode(),d));
		} else if(loanClaim.getTimeType().equals("D")){
			loanClaim.setExpireDate(DateUtil.getNextDayTime(loanClaim.getLoanTerm()-1, DictConstant.TimeFormat.SHORTTIME.getCode(), new Date()));
		}
		loanClaimDao.updateByPrimaryKeySelectiveByVersion(loanClaim);
		
		BigDecimal serviceFee = new BigDecimal("0");
		//借款来源：APP、渠道（结标扣除服务费）
		if(feeType.equals("CHANNEL")){
			serviceFee = saveAndGetServiceFee(lc, platForm);
		}
		investTransDao.selectListByProjectNo(lc.getProjectNo());
		JsonResult json = saveCommon(lc.getProjectNo(), loanClaim.getCustId(), loanClaim.getLoanAmount(), investTransDao.selectListByProjectNo(lc.getProjectNo()), platForm, serviceFee);
		if(json.isSuccess()){
			//生成还款计划
			Repay repay = new Repay();
			LoanInfoVo lv = new LoanInfoVo();
			lv.setRepayTotalAmount(loanClaim.getLoanAmount());
			lv.setRepayTerm(loanClaim.getLoanTerm());
			lv.setRepayTermType(loanClaim.getTimeType());
			lv.setRepayMethod(loanClaim.getRepayMethod());
			lv.setRepayRate(loanClaim.getInterestRate());
			lv.setLoanDate(loanClaim.getLoanDate());
			lv.setPlatformFeeMethod("1");// 平台服务费收取方式: 1.一次性预收 2.每月收取
			lv.setPlatformFeeRate(loanClaim.getPlatformRate());
			lv.setOverduePlatformFeeRate(loanClaim.getOverdueRate());
			lv.setRepayEarlierFeeRate(loanClaim.getEarlyPayRate());
			lv.setCustId(loanClaim.getCustId());
			lv.setProjectNo(loanClaim.getProjectNo());
			lv.setRateType(loanClaim.getTimeType().equals("M")?"年":(loanClaim.getTimeType().equals("D"))?"月":""); // 年,月
			List<RepayPlan> planList = repay.createRepayPlan(lv);
			for(RepayPlan plan : planList){
				if(EmptyUtil.isNotEmpty(plan)){
					repayPlanDao.insertSelective(plan);
				}
			}
			result.setSuccess(true);
		} else {
			result.setMsg(json.getMsg());
		}
		return result;
	}

	
	public JsonResult saveCommon(String projectNo, long loanUserId, BigDecimal amount, List<InvestTrans> transList,String platForm, BigDecimal serviceFee) throws Exception {
		JsonResult result = new JsonResult();
		LoanClaim loanClaim = loanClaimDao.selectByPrimaryKey(projectNo);
		BigDecimal sumAmount = amount;//借款金额或债权转让本金
		String repayMenthod = loanClaim.getRepayMethod(); //还款方式:等额本息,先息后本,到期还本付息
		CustClaim custClaim = null;
		for(InvestTrans trans : transList){
			InvestProdRecord investProdRecord = investProdRecordDao.selectByPrimaryKey(trans.getRecordNo());
			BigDecimal investAmount = trans.getInvestAmount();
			BigDecimal investPrice = trans.getInvestPrice();
			long custId = investProdRecord.getCustId();
			String loginName = investProdRecord.getCustLoginName();
			custClaim = createCustClaim(custClaim, loginName, custId, loanClaim, investProdRecord, trans);
			Date d = DateUtil.getNextMonthTime(1, DictConstant.TimeFormat.SHORTTIME.getCode());
			custClaim.setNextRepayDate(DateUtil.getNextDayTime(-1, DictConstant.TimeFormat.SHORTTIME.getCode(),d));
			//计算待收收益
			BigDecimal receivingInterest = new BigDecimal("0");
			//投资账户划转
			CustAccount account = custAccountDao.selectCustAcctByIdAndStatus(custId, null);
			if(!account.getAccountStatus().equals(DictConstant.CustStatus.NORMAL.getCode())){
				throw new Exception("投资账户【"+account.getCustId()+"】非正常状态。【"+new Date()+"】");
			}
			account.setUpdateTime(new Date());
			account.setForzenAmount(account.getForzenAmount().subtract(investPrice));
			account.setCollectingPrincipal(account.getCollectingPrincipal().add(investAmount));
			BaseAccount baseAccount = baseAccountDao.selectByPrimaryKey(account.getAccountNo());
			AccountFlow flow=new AccountFlow();
			flow.setSerialNo(StringUtil.GetUniquelyString());
			flow.setAcctNo(account.getAccountNo());
			flow.setAcctName(baseAccount.getAcctName()); //账户名
			flow.setTransType(DictConstant.transactionType.TRANS_OUT.getCode());
			flow.setTransAmount(investPrice);
			flow.setAcctBal(account.getUsableBalance());
			flow.setTransTime(new Date());
			flow.setDealStatus(DictConstant.flowDealStatus.SUCCESS.getCode());
			flow.setAcctKind(DictConstant.ACCOUNT_KIND.CUST.getCode().toString());
			flow.setProjectNo(projectNo);
			flow.setCreditNo(custClaim.getCreditNo());
			flow.setFreezeAmount(account.getForzenAmount());
			if(platForm.equals("APP")){
				flow.setTransChl(DictConstant.platForm.APP.getCode().toString());
			} else if(platForm.equals("P2P")){
				flow.setTransChl(DictConstant.platForm.P2P.getCode().toString());
			}
			flow.setReqNo("yeepay"); // yeepay
			accountFlowDao.insertSelective(flow);
			try {
				//AccountFlow af = accountFlowDao.selectAccountFlowByRecordNo(trans.getRecordNo());
				YeePayLogInfo yeePayLog = yeePayLogDao.selectByUserNo(account.getAccountNo());
				//yeepay调用
				CompleteTransactionRequest request=new CompleteTransactionRequest();
				request.setMode("CONFIRM");
				request.setRequestNo(yeePayLog.getRquestNo());
				DirectTransactionResponse response = yeePayService.doCompleteTransaction(request);// 转账确认
				if(!response.getCode().equals("1")){
					// TODO 处理回调信息
				}
			} catch (Exception e) {
				System.out.println("yeepay划账异常");
			}
			//生成回款计划、记录
			ReceivePlan plan = new ReceivePlan();
			plan.setPaymentStatus(DictConstant.REPAY_STATUS.WAIT_REPAY.getCode());
			plan.setDelFlag(DictConstant.FlagStatus.N.toString());
			plan.setClaimNo(custClaim.getCreditNo());
			plan.setProjectNo(projectNo);
			plan.setCustId(custId);
			
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("productCode", investProdRecord.getProdCode());
			paramMap.put("oldProdStatus", "RELEASED");
			
			PlanProduct planProduct = planProductDao.getInvestPlanByParam(paramMap);
			
			if(EmptyUtil.isNotEmpty(planProduct)&&planProduct.getTermType().equals(DictConstant.ProductType.PROD_NEW.getCode())&&investProdRecord.getTermType().equals("D")){
				//新客专享 只有一条 回款记录、计划
				BigDecimal interest = CostCalculatorHelper.dayInterest(investProdRecord.getInvestTerm(), investAmount, investProdRecord.getYearRate());
				plan.setTermNo(1);
				plan.setCapital(investAmount);
				plan.setInterest(interest);
				plan.setRepaymentDate(DateUtil.getNextDayTime(investProdRecord.getInvestTerm()-1, DictConstant.TimeFormat.SHORTTIME.getCode(), new Date()));
				plan.setRemainCapital(plan.getCapital());
				plan.setRemainInterest(plan.getInterest());
				plan.setCreateTime(new Date());
				receivePlanDao.insertSelective(plan);
				receivingInterest = interest;
			} else {
				if(repayMenthod.equals("LCI")&&investProdRecord.getTermType().equals("M")){ // 到期还本付息
					// 只有一条 回款记录、计划
					BigDecimal interest = CostCalculatorHelper.disposableInterest(investAmount, investProdRecord.getYearRate(), new BigDecimal(investProdRecord.getInvestTerm()));
					plan.setTermNo(1);
					plan.setCapital(investAmount);
					plan.setInterest(interest);
					Date date = DateUtil.getNextMonthTime(investProdRecord.getInvestTerm(), DictConstant.TimeFormat.SHORTTIME.getCode());
					plan.setRepaymentDate(DateUtil.getNextDayTime(-1, DictConstant.TimeFormat.SHORTTIME.getCode(),date));
					plan.setRemainCapital(plan.getCapital());
					plan.setRemainInterest(plan.getInterest());
					plan.setCreateTime(new Date());
					receivePlanDao.insertSelective(plan);
					receivingInterest = interest;
				} else if(investProdRecord.getTermType().equals("M")){
					BigDecimal monthlyAmount = new BigDecimal("0");
					BigDecimal FISCInterest = new BigDecimal("0");
					if(repayMenthod.equals("ACPI")){//等额本息
						//等额本息----每月还款总额
						monthlyAmount = CostCalculatorHelper.monthlyAmount(investAmount, investProdRecord.getYearRate(), new BigDecimal(investProdRecord.getInvestTerm()));
						receivingInterest = monthlyAmount.multiply(new BigDecimal(investProdRecord.getInvestTerm())).subtract(investAmount);
					} else if(repayMenthod.equals("FISC")){//先息后本
						//先息后本----总利息
						FISCInterest = CostCalculatorHelper.disposableInterest(investAmount, investProdRecord.getYearRate(), new BigDecimal(investProdRecord.getInvestTerm()));
						receivingInterest = FISCInterest;
					} else if(repayMenthod.equals("ECEI")){
						// TODO 等本等息(530不做)
					} else {
						new Exception("还款方式错误【"+repayMenthod+"】,【"+new Date()+"】");
					}
					for(int i=1;i<=investProdRecord.getInvestTerm();i++){
						plan.setId(null);
						plan.setTermNo(i);
						if(repayMenthod.equals("ACPI")){//等额本息
							BigDecimal ACPIInterest = CostCalculatorHelper.monthlyInterest(investAmount, investProdRecord.getYearRate(), new BigDecimal(investProdRecord.getInvestTerm()), new BigDecimal(i));
							plan.setCapital(monthlyAmount.subtract(ACPIInterest));
							plan.setInterest(ACPIInterest);
							Date date = DateUtil.getNextMonthTime(i, DictConstant.TimeFormat.SHORTTIME.getCode());
							plan.setRepaymentDate(DateUtil.getNextDayTime(-1, DictConstant.TimeFormat.SHORTTIME.getCode(),date));
						} else if(repayMenthod.equals("FISC")){//先息后本
							plan.setInterest(FISCInterest.divide(new BigDecimal(investProdRecord.getInvestTerm()), 2, BigDecimal.ROUND_HALF_DOWN));
							plan.setCapital(investProdRecord.getInvestTerm()==i?investAmount:BigDecimal.ZERO);
							Date date = DateUtil.getNextMonthTime(i, "yyyy-MM-dd");
							plan.setRepaymentDate(DateUtil.getNextDayTime(-1, "yyyy-MM-dd",date));
						} else if(repayMenthod.equals("ECEI")){
							// TODO 等本等息(530不做)
						} else {
							new Exception("还款方式错误【"+repayMenthod+"】,【"+new Date()+"】");
						}
						plan.setRemainCapital(plan.getCapital());
						plan.setRemainInterest(plan.getInterest());
						plan.setCreateTime(new Date());
						receivePlanDao.insertSelective(plan);
					}
				} else if(investProdRecord.getTermType().equals("D")){
					// APP 按天计息产品  只有一条 回款记录、计划 
					BigDecimal interest = CostCalculatorHelper.dayInterest(investProdRecord.getInvestTerm(), investAmount, investProdRecord.getYearRate());
					plan.setTermNo(1);
					plan.setCapital(investAmount);
					plan.setInterest(interest);
					plan.setRepaymentDate(DateUtil.getNextDayTime(investProdRecord.getInvestTerm()-1, DictConstant.TimeFormat.SHORTTIME.getCode(), new Date()));
					plan.setRemainCapital(plan.getCapital());
					plan.setRemainInterest(plan.getInterest());
					plan.setCreateTime(new Date());
					receivePlanDao.insertSelective(plan);
					receivingInterest = interest;
				} else {}
			}
			account.setCollectingRevenue(account.getCollectingRevenue().add(receivingInterest));
			custClaim.setReceivingInterest(receivingInterest);
			custClaimDao.insertSelective(custClaim);
			custAccountDao.updateByVersion(account);
		}
		
		//扣除服务费后入账金额
		//sumAmount = sumAmount.subtract(serviceFee);
		//TODO 服务费处理
		
		
		// 借款入账
		CustAccount account = custAccountDao.selectCustAcctByIdAndStatus(loanUserId, null);
		if(!account.getAccountStatus().equals(DictConstant.CustStatus.NORMAL.getCode())){
			throw new Exception("借款人账户【"+account.getCustId()+"】非正常状态。【"+new Date()+"】");
		}
		account.setUpdateTime(new Date());
		account.setUsableBalance(account.getUsableBalance().add(sumAmount));
		BaseAccount baseAccount = baseAccountDao.selectByPrimaryKey(account.getAccountNo());
		baseAccount.setAcctBal(baseAccount.getAcctBal().add(sumAmount));
		AccountFlow flow = new AccountFlow();
		flow.setSerialNo(StringUtil.GetUniquelyString());
		flow.setAcctNo(account.getAccountNo());
		flow.setAcctName(baseAccount.getAcctName()); //账户名
		flow.setTransType(DictConstant.transactionType.TRANS_IN.getCode());
		flow.setTransAmount(sumAmount);
		flow.setAcctBal(account.getUsableBalance());
		flow.setTransTime(new Date());
		flow.setDealStatus(DictConstant.flowDealStatus.SUCCESS.getCode());
		flow.setAcctKind(DictConstant.ACCOUNT_KIND.CUST.getCode().toString());
		flow.setProjectNo(projectNo);
		flow.setCreditNo(custClaim.getCreditNo());
		flow.setFreezeAmount(account.getForzenAmount());
		if(platForm.equals("APP")){
			flow.setTransChl(DictConstant.platForm.APP.getCode().toString());
		} else if(platForm.equals("P2P")){
			flow.setTransChl(DictConstant.platForm.P2P.getCode().toString());
		}
		flow.setReqNo("yeepay"); // TODO yeepay
		accountFlowDao.insertSelective(flow);
		custAccountDao.updateByVersion(account);
		baseAccountDao.updateByVersion(baseAccount);
		
		result.setSuccess(true);
		result.setMsg("成功");
		return result;
	}
	
	public CustClaim createCustClaim(CustClaim custClaim,String loginName,long custId,LoanClaim loanClaim,InvestProdRecord investProdRecord,InvestTrans trans) throws Exception{
		String newCreditNo=trans.getNewCreditNo();//债权转让时，上游已生成
		if(newCreditNo==null){
			newCreditNo=sequenceService.getCompleteSequenceBySeqKey("ZQBH");
		}
		custClaim.setCreditNo(newCreditNo);
		custClaim.setProjectNo(loanClaim.getProjectNo());//项目编号
		custClaim.setCustId(custId);
		custClaim.setInvestAmount(trans.getInvestAmount());
		custClaim.setRemainAmount(trans.getInvestAmount());
		custClaim.setInvestTime(trans.getInvestTime());
		custClaim.setInvestMethod(trans.getInvestMethod());
		custClaim.setRecordNo(trans.getRecordNo());
		custClaim.setProdCode(investProdRecord.getProdCode());
		custClaim.setProdName(investProdRecord.getProdName());
		custClaim.setIsTransfer("N");//TODO 直投产品根据产品设置
		custClaim.setCustLoginName(loginName);
		custClaim.setYearRate(investProdRecord.getYearRate());
		custClaim.setProjectName(loanClaim.getProjectName());
		custClaim.setClaimPercent(trans.getInvestAmount().divide(loanClaim.getLoanAmount(),Constants.CURRENCY_DB_SCALE,RoundingMode.DOWN)); //债权占比
		custClaim.setExpireDate(loanClaim.getExpireDate());
		Date d = DateUtil.getNextMonthTime(1, DictConstant.TimeFormat.SHORTTIME.getCode());
		custClaim.setNextRepayDate(DateUtil.getNextDayTime(-1, DictConstant.TimeFormat.SHORTTIME.getCode(),d));
		return custClaim;
	}
	/**
	 * 收取服务费
	 */
	public BigDecimal saveAndGetServiceFee(LoanClaim lc,String platForm) throws Exception{
		LoanClaim loanClaim = loanClaimDao.selectByPrimaryKey(lc.getProjectNo());
		String projectNo = loanClaim.getProjectNo();
		//投资总额
		BigDecimal serviceFee = new BigDecimal("0");
		
		// serviceFee 划转服务费，借款人-->牛蛙账户
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("acctType", "DEPOSIT");
		paramMap.put("useType", DictConstant.NW_ACCOUNT_USE.CHARGE.getCode());
		NwAccount nwAccount = nwAccountDao.selectAcctByCriteria(paramMap);
		if(EmptyUtil.isEmpty(nwAccount)){
			throw new Exception("NW账户不存在。【"+new Date()+"】");
		}
		try {
			// TODO 平台收取借款人服务费
			//CompleteTransactionRequest request=new CompleteTransactionRequest();
			//DirectTransactionResponse response=yeePayService.doCompleteTransaction(request);
			/*AutoTransactionRequest request = new AutoTransactionRequest();
			request.setPlatformNo("");//[必须]
			request.setUserType("");//出款人用户类型 [必须]
			request.setBizType(""); //业务类型 [必须]
			AutoTransactionDetail detail = new AutoTransactionDetail();
			detail.setAmount(serviceFee.toString());// 转入金额 【必须】
			detail.setTargetUserType("MERCHANT");// 用户类型 个人 MEMBER 商户 MERCHANT 【必须】
			detail.setTargetPlatformUserNo(nwAccount.getAcctNo());// 平台用户编号 【必须】
			//detail.setBizType(bizType);// 资金明细业务类型 【必须】
			request.setDetail(detail);
			DirectTransactionResponse response = yeePayService.doAutoTransaction(request);*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取借款服务费
		serviceFee = CalculateBusiUtils.LoanServiceCharge(loanClaim.getLoanAmount(), loanClaim.getPlatformRate(), loanClaim.getLowPlatformFee());
		nwAccount.setAcctBal(nwAccount.getAcctBal().add(serviceFee));
		AccountFlow flow = new AccountFlow();
		flow.setSerialNo(StringUtil.GetUniquelyString());
		flow.setAcctNo(nwAccount.getAcctNo());
		flow.setAcctName(nwAccount.getAcctName()); //账户名
		flow.setTransType(DictConstant.transactionType.TRANS_IN.getCode());
		flow.setTransAmount(serviceFee);
		flow.setAcctBal(nwAccount.getAcctBal());
		flow.setTransTime(new Date());
		flow.setDealStatus(DictConstant.flowDealStatus.SUCCESS.getCode());
		flow.setAcctKind(DictConstant.ACCOUNT_KIND.NW.getCode().toString());
		flow.setProjectNo(projectNo);
		//flow.setCreditNo(custClaim.getCreditNo());
		if(platForm.equals("APP")){
			flow.setTransChl(DictConstant.platForm.APP.getCode().toString());
		} else if(platForm.equals("P2P")){
			flow.setTransChl(DictConstant.platForm.P2P.getCode().toString());
		}
		flow.setReqNo("yeepay"); // TODO yeepay
		accountFlowDao.insertSelective(flow);
		nwAccountDao.updateNWAccount(nwAccount); //byVersion TODO
		return serviceFee;
	}
}
